import {timeUtil} from '../../../util'

var methodsHelper = {
  init: {
    // 参数校验
    validatePageParams: function (route, pageControl) {
      // 1 非空校验
      if (route.params === undefined) {
        console.error('route.params is undefined')
        return false
      }
      if (route.params.operateType === undefined) {
        console.error('route.params.operateType is undefined')
        return false
      }
      if (route.params.identity === undefined) {
        console.error('route.params.identity is undefined')
        return false
      }
      if (route.params.approvalStep === undefined) {
        console.error('route.params.approvalStep is undefined')
        return false
      }
      if (route.params.appCode === undefined) {
        console.error('route.params.appCode is undefined')
        return false
      }
      // 2 参数值-非法校验
      if (pageControl.operateType[route.params.operateType] === undefined) {
        console.error('route.params.operateType is unavailable')
        return false
      }
      if (pageControl.identity[route.params.identity] === undefined) {
        console.error('route.params.identity is unavailable')
        return false
      }
      if (pageControl.approveStep[route.params.approvalStep] === undefined) {
        console.error('route.params.approvalStep is unavailable')
        return false
      }
      // 3 参数逻辑-非法校验
      // 3.1 运营人员 & 非预览操作 => 非法操作
      if (pageControl.identity[route.params.identity] === pageControl.identity.operator && (pageControl.operateType[route.params.operateType] !== pageControl.operateType.preview || pageControl.approveStep[route.params.approvalStep] !== pageControl.approveStep.for_search)) {
        console.error('Illegal Request!! Operator only allowed to Preview the For_Search refs, change your operateType to preview and approveStep to for_search then it would work')
        return false
      }
      // 3.2 编辑操作 & 非复审阶段 => 非法操作
      if (pageControl.operateType[route.params.operateType] === pageControl.operateType.modify && pageControl.approveStep[route.params.approvalStep] !== pageControl.approveStep.review) {
        console.error('Illegal Request!! No Modify on approveStep except Review, change your approveStep to review then it would work')
        return false
      }
      // 3.3 综合查询 & 非预览操作 => 非法操作
      if (pageControl.approveStep[route.params.approvalStep] === pageControl.approveStep.for_search && pageControl.operateType[route.params.operateType] !== pageControl.operateType.preview) {
        console.error('Illegal Request!! No For_Search on operateType except Preview, change your operateType to preview then it would work')
        return false
      }
      return true
    },
    // 保存参数
    savePageParam: function (pageParams, route) {
      pageParams.operateType = route.params.operateType // 操作类型
      pageParams.identity = route.params.identity // 身份标识
      pageParams.approvalStep = route.params.approvalStep // 信审流程阶段
      pageParams.appCode = route.params.appCode // 订单号
    },
    // 操作相关设置
    operateTypeRelateSettings: function (pageControl, cardConfig) {
      if (pageControl.current.operateType === pageControl.operateType.preview) {
        // 操作类型 - 是否预览操作
        pageControl.current.operateTypePreview = true
      } else if (pageControl.current.operateType === pageControl.operateType.approve) {
        // 操作类型 - 是否审批操作
        pageControl.current.operateTypeApprove = true
        // 操作类型 - 如果是审批操作，则设置preview为true
        pageControl.current.operateTypePreview = true
      } else if (pageControl.current.operateType === pageControl.operateType.modify) {
        // 操作类型 - 是否编辑操作
        pageControl.current.operateTypeModify = true
      }

      // 操作类型 - 如果预览属性开启，则显示顶部的detailsButton
      if (pageControl.current.operateTypePreview) {
        cardConfig.detailButton.text.current = cardConfig.detailButton.text.hide
        cardConfig.collapseButton.text.current = cardConfig.collapseButton.text.hide
      }
    },
    // 身份相关设置
    identityRelateSettings: function (pageControl) {
      if (pageControl.current.identity === pageControl.identity.approver) {
        // 操作人身份 - 是否审批人
        pageControl.current.identityApprover = true
      }
    },
    // 流程阶段相关设置
    approvalStepRelateSettings: function (pageControl, apiUrl, cardConfig, pageTitle, pageParams) {
      // 【重点】pageControl.current.approvalStep 仅用于init中逻辑判断，其他逻辑均用对应的Boolean类型做判断（原因：1.代码简介；2.终审和终审复议时该值是相同的）
      if (pageControl.current.approvalStep === pageControl.approveStep.first) {
        // 信审流程阶段 - 是否初审阶段
        pageControl.current.approvalStepFirst = true
        // 返回页面
        apiUrl.returnPage.current = apiUrl.returnPage.first
        // 提交接口
        apiUrl.approve.currentObj = apiUrl.approve.first
      } else if (pageControl.current.approvalStep === pageControl.approveStep.review) {
        // 信审流程阶段 - 是否复审阶段
        pageControl.current.approvalStepReview = true
        // 返回页面
        apiUrl.returnPage.current = apiUrl.returnPage.review
        // 提交接口
        apiUrl.approve.currentObj = apiUrl.approve.review
      } else if (pageControl.current.approvalStep === pageControl.approveStep.final) {
        // 信审流程阶段 - 是否终审阶段
        pageControl.current.approvalStepFinal = true
        // 返回页面
        apiUrl.returnPage.current = apiUrl.returnPage.final
        // 提交接口
        apiUrl.approve.currentObj = apiUrl.approve.final
      } else if (pageControl.current.approvalStep === pageControl.approveStep.reconsider) {
        // 信审流程阶段 - 是否终审阶段 - 终审复议
        pageControl.current.approvalStepReconsider = true
        // 返回页面
        apiUrl.returnPage.current = apiUrl.returnPage.final
        // 提交接口
        apiUrl.approve.currentObj = apiUrl.approve.reconsider
      } else if (pageControl.current.approvalStep === pageControl.approveStep.contract) {
        // 信审流程阶段 - 是否签约审阶段
        pageControl.current.approvalStepContract = true
        // 返回页面
        apiUrl.returnPage.current = apiUrl.returnPage.contract
        // 提交接口
        apiUrl.approve.currentObj = apiUrl.approve.contract
      } else if (pageControl.current.approvalStep === pageControl.approveStep.for_search) {
        // 综合查询
        pageControl.current.forSearch = true
        apiUrl.returnPage.current = apiUrl.returnPage.forSearch
      } else if (pageControl.current.approvalStep === pageControl.approveStep.gps) {
        // GPS查询
        pageControl.current.approvalStepGps = true
        apiUrl.returnPage.current = apiUrl.returnPage.gps
        // 提交接口
        apiUrl.approve.currentObj = apiUrl.approve.gps
      }

      // 设置编辑页的返回页
      if (pageControl.current.operateTypeModify) {
        apiUrl.returnPage.current = apiUrl.returnPage.modify + pageParams.appCode
      }

      // 信审流程阶段 - 是否终审阶段之后
      if (!(pageControl.current.approvalStep in [pageControl.approveStep.first, pageControl.approveStep.review, pageControl.approveStep.final])) {
        pageControl.current.approvalStepAfterFinal = true
      }

      // 如果当前为终审复议，设置step为final [这里决定了终审和终审复议时approveStep值相同]
      if (pageControl.current.approvalStepReconsider) {
        pageControl.current.approvalStep = pageControl.approveStep.final
      }

      // 页面标题 - 当前
      if (pageControl.current.operateTypeModify) {
        cardConfig.collapseButton.text.current = cardConfig.collapseButton.text.hide
        pageTitle.current = pageTitle.modify
      } else {
        pageTitle.current = pageTitle[pageParams.approvalStep]
      }
    }
  },
  calculateFee: {
    prepareParams: function (appCode, pageData) {
      var params = {}
      params.appCode = appCode
      params.carLoanAmount = pageData.orderBaseInfo.rCarloanAmount
      params.loanPeriods = pageData.orderBaseInfo.rLoanPeriods
      params.carFirstBook = pageData.orderCarInfo.carFirstBook
      params.carMiles = pageData.orderCarInfo.carMiles
      params.carNewPrice = pageData.orderCarInfo.carNewPrice
      params.comRate = pageData.orderLoanInfo.aComRate
      params.lifeRate = pageData.orderLoanInfo.alifeInsuranceRate
      params.dealerCode = pageData.orderBaseInfo.dealerCode
      params.gpsLevel = pageData.orderLoanInfo.gpsLevel
      params.isHouse = pageData.orderLoanInfo.isHouse
      params.isLcv = pageData.orderLoanInfo.isLcv
      params.isOld = pageData.orderCarInfo.isOld
      params.productCode = pageData.orderBaseInfo.productCode
      params.rateLevel = pageData.orderLoanInfo.rateLevel
      params.lifeLevel = pageData.orderLoanInfo.lifeInsuranceLevel
      params.salePrice = pageData.orderLoanInfo.salePrice
      return params
    },
    dealResponse: function (reply, pageData, pageHelperData) {
      pageData.orderBaseInfo.rAccountFee = reply.accountAmount
      pageData.orderBaseInfo.rlifeInsuranceCostFee = reply.alifeInsuranceCostFee
      pageData.orderBaseInfo.rlifeInsuranceProfitFee = reply.alifeInsuranceProfitFee
      pageData.orderLoanInfo.alifeInsuranceRate = reply.alifeInsuranceRate
      pageData.orderBaseInfo.rBusinessInsurance = reply.businessInsurance
      pageData.orderBaseInfo.rComFee = reply.comFee
      pageData.orderLoanInfo.disaRate = reply.disaRate
      pageData.orderLoanInfo.discountE = reply.discountE
      pageData.orderLoanInfo.discountTrueE = reply.discountTrueE
      pageData.orderBaseInfo.rExtendCost = reply.extendCost
      pageHelperData.form.extendName = reply.extendName
      pageData.orderLoanInfo.gpsCount = reply.gpsCount
      pageData.orderBaseInfo.rGpsFee = reply.gpsFee
      pageData.orderLoanInfo.gpsPro = reply.gpsPro
      pageData.orderBaseInfo.gpsRebate = reply.gpsRebate
      pageData.orderBaseInfo.rInitPayment = reply.initPayment
      pageData.orderBaseInfo.rInitScale = reply.initScale
      pageData.orderLoanInfo.lifeInsuranceLevel = reply.lifeInsuranceLevel
      pageData.orderBaseInfo.rLoanAmount = reply.loanAmount
      pageData.orderBaseInfo.rLoanRate = reply.loanRate
      pageData.orderBaseInfo.rGrossInterest = reply.preInterestAmount
      pageData.orderBaseInfo.serFinRebate = reply.rebateFee
      pageData.orderBaseInfo.rSecureFee = reply.secureFee
      pageData.orderBaseInfo.rTaxPrice = reply.taxPrice
      pageData.orderBaseInfo.rTrafficInsurance = reply.trafficInsurance
    }
  },
  calculateMonthPay: {
    prepareParams: function (pageData) {
      var params = {}
      params.amount = pageData.orderBaseInfo.rLoanAmount
      params.periods = pageData.orderBaseInfo.rLoanPeriods
      params.yearRate = pageData.orderBaseInfo.rLoanRate
      params.productCode = pageData.spdBusinessInfo.cardProductCode
      return params
    }
  },
  saveReplyData: {
    prepareParams: function (pageParams, pageData) {
      var params = {}
      params.appCode = pageParams.appCode
      params.rCarLoanAmount = pageData.orderBaseInfo.rCarloanAmount
      params.rLoanPeriods = pageData.orderBaseInfo.rLoanPeriods
      params.rAccountFee = pageData.orderBaseInfo.rAccountFee
      params.rRateLevel = pageData.orderBaseInfo.rRateLevel
      params.rComFee = pageData.orderBaseInfo.rComFee
      params.rYanbaoFee = pageData.orderBaseInfo.rYanbaoFee
      params.rGpsFee = pageData.orderBaseInfo.rGpsFee
      params.rLoanRate = pageData.orderBaseInfo.rLoanRate
      params.preloanRate = pageData.orderBaseInfo.rLoanRate
      params.rInitPayment = pageData.orderBaseInfo.rInitPayment
      params.rInitScale = pageData.orderBaseInfo.rInitScale
      params.rDiscountTrueE = pageData.orderBaseInfo.rDiscountTrueE
      params.rExtendCost = pageData.orderBaseInfo.rExtendCost
      params.rTaxPrice = pageData.orderBaseInfo.rTaxPrice
      params.rBusinessInsurance = pageData.orderBaseInfo.rBusinessInsurance
      params.rTrafficInsurance = pageData.orderBaseInfo.rTrafficInsurance
      params.rSecureFee = pageData.orderBaseInfo.rSecureFee
      params.isSpouseSign = pageData.orderBaseInfo.isSpouseSign
      params.isGuaranteedSign = pageData.orderBaseInfo.isGuaranteedSign
      params.isPrintW = pageData.orderBaseInfo.isPrintW
      params.rlifeInsuranceProfitFee = pageData.orderBaseInfo.rlifeInsuranceProfitFee
      params.rlifeInsuranceCostFee = pageData.orderBaseInfo.rlifeInsuranceCostFee
      params.serFinRebate = pageData.orderBaseInfo.serFinRebate
      params.rGrossInterest = pageData.orderBaseInfo.rGrossInterest
      params.gpsRebate = pageData.orderBaseInfo.gpsRebate
      params.gpsCount = pageData.orderLoanInfo.gpsCount
      params.gpsPro = pageData.orderLoanInfo.gpsPro
      return params
    }
  },
  modifyOrder: {
    prepareParams: function (pageParams, pageData, pageHelperData) {
      var modifyParam = {}
      // 订单号
      modifyParam.appCode = pageParams.appCode
      // 申请人信息
      modifyParam.proppserInfoParam = Object.assign({}, pageData.orderProppserInfo)
      modifyParam.proppserInfoParam.cardProductCode = pageData.spdBusinessInfo.cardProductCode
      modifyParam.proppserInfoParam.cardProductName = pageData.spdBusinessInfo.cardProductName
      modifyParam.proppserInfoParam.email = pageData.spdBusinessInfo.email
      modifyParam.proppserInfoParam.firstWorkTime = pageData.proppserWorkInfo.firstWorkTime
      modifyParam.proppserInfoParam.nowUnitAddress = pageData.proppserWorkInfo.nowUnitAddress
      modifyParam.proppserInfoParam.nowUnitCity = pageData.proppserWorkInfo.nowUnitCity
      modifyParam.proppserInfoParam.nowCompany = pageData.proppserWorkInfo.nowCompany
      modifyParam.proppserInfoParam.nowUnitDistrict = pageData.proppserWorkInfo.nowUnitDistrict
      modifyParam.proppserInfoParam.nowUnitKind = pageData.proppserWorkInfo.nowUnitKind
      modifyParam.proppserInfoParam.nowUnitProvince = pageData.proppserWorkInfo.nowUnitProvince
      modifyParam.proppserInfoParam.nowUnitScale = pageData.proppserWorkInfo.nowUnitScale
      modifyParam.proppserInfoParam.nowUnitTel = pageData.proppserWorkInfo.nowUnitTel
      modifyParam.proppserInfoParam.nowWorkYear = pageData.proppserWorkInfo.nowWorkYear
      // 车辆信息
      modifyParam.carInfoParam = Object.assign({}, pageData.orderCarInfo)
      modifyParam.carInfoParam.carHomePrice = pageData.orderCarInfo.carHomePrice
      modifyParam.carInfoParam.floatingPro = pageData.orderCarInfo.floatingPro
      modifyParam.carInfoParam.vinIdentifyChe300Id = pageData.orderCarInfo.vinIdentifyChe300Id
      // 担保人信息
      modifyParam.guaranteeInfoParam = Object.assign({}, pageData.guaranteeInfo)
      // 配偶信息
      modifyParam.relativesInfoParam = Object.assign({}, pageData.orderRelativeInfo)
      // 备注信息
      modifyParam.loanRemarks = pageHelperData.loanRemarks // 复审审批备注
      modifyParam.fyRemarks = pageHelperData.fyRemarks // 复议备注
      return modifyParam
    }
  },
  evaluatePrice: {
    prepareParams: function (dialogData, pageData, pageHelperData, _enumStore) {
      dialogData.getC300EvaluationPrice.apiParams.dealerCode = pageData.orderBaseInfo.dealerCode
      dialogData.getC300EvaluationPrice.apiParams.vin = pageData.orderCarInfo.carIdentify
      dialogData.getC300EvaluationPrice.apiParams.mileage = pageData.orderCarInfo.carMiles
      dialogData.getC300EvaluationPrice.apiParams.firstregtime = timeUtil.parseTime(pageData.orderCarInfo.carFirstBook, '{y}-{m}-{d}')
      dialogData.getC300EvaluationPrice.apiParams.che300Mid = pageData.orderCarInfo.vinIdentifyChe300Id
      dialogData.getC300EvaluationPrice.apiParams.mid = pageData.orderCarInfo.carStyleId
      dialogData.getC300EvaluationPrice.apiParams.color = _enumStore.state['车辆颜色'][pageData.orderCarInfo.carColor]
    }
  },
  reset: {
    approveForm: function (form) {
      // 公用审批数据
      form.passType = null
      form.passReason = null
      form.refuseType = null
      form.refuseReason = null
      form.cancelType = null
      form.riskCode = null
      form.riskName = null
      form.backCode = null
      form.backType = null
      form.backReason = null
      form.remarks = null
      form.nextUserId = null
      form.nextTrueName = null
      // 签约数据
      form.carResult = null
      form.isCreditReport = null
      form.authCh = null
      form.pledModelId = null
      form.isVipContract = null
      form.modelIds = null
      form.transferRegDate = null
    }
  }
}

export {
  methodsHelper
}
